package com.ysy.tz.core.print;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class BarcodePrint {
    static BarcodePrint barcodePrint = null;

    public BarcodePrint() {
    }

    public static BarcodePrint getInstance() {
        if (barcodePrint == null) {
            barcodePrint = new BarcodePrint();
        }

        return barcodePrint;
    }

    public void printGradingInfo(String userName, String billNo, String weight, String gradingLevel, String barCode) {
        System.setProperty("jna.encoding", "GBK");
        BarcodePrint.TscLibDll.INSTANCE.openport("TSC");
        BarcodePrint.TscLibDll.INSTANCE.setup("100", "100", "4", "10", "0", "2", "0");
        BarcodePrint.TscLibDll.INSTANCE.clearbuffer();
        BarcodePrint.TscLibDll.INSTANCE.sendcommand("KILL \"*.GK\"");
        BarcodePrint.TscLibDll.INSTANCE.sendcommand("PUTPCX 3,0,\"TMDY.pcx\"");
        BarcodePrint.TscLibDll.INSTANCE.windowsfont(50, 50, 50, 0, 0, 0, "黑体", "人员:" + userName);
        BarcodePrint.TscLibDll.INSTANCE.windowsfont(50, 200, 50, 0, 0, 0, "黑体", "序号:" + billNo);
        BarcodePrint.TscLibDll.INSTANCE.windowsfont(50, 350, 50, 0, 0, 0, "黑体", "重量:" + weight);
        BarcodePrint.TscLibDll.INSTANCE.windowsfont(50, 500, 50, 0, 0, 0, "黑体", "等级:" + gradingLevel);
        BarcodePrint.TscLibDll.INSTANCE.barcode("50", "650", "128", "120", "0", "0", "8", "8", barCode);
        BarcodePrint.TscLibDll.INSTANCE.windowsfont(200, 850, 100, 0, 0, 0, "黑体", barCode);
        BarcodePrint.TscLibDll.INSTANCE.printlabel("1", "1");
        BarcodePrint.TscLibDll.INSTANCE.closeport();
    }

    public static String toMultiLine(String str, int len) {
        char[] chs = str.toCharArray();
        StringBuffer sb = new StringBuffer();
        int i = 0;

        for(int sum = 0; i < chs.length; ++i) {
            sum += chs[i] < 255 ? 1 : 2;
            sb.append(chs[i]);
            if (sum >= len) {
                sum = 0;
                sb.append("&&");
            }
        }

        return sb.toString();
    }

    public interface TscLibDll extends Library {
        TscLibDll INSTANCE = (TscLibDll)Native.loadLibrary("TSCLIB", TscLibDll.class);

        int about();

        int openport(String var1);

        int closeport();

        int sendcommand(String var1);

        int setup(String var1, String var2, String var3, String var4, String var5, String var6, String var7);

        int downloadpcx(String var1, String var2);

        int barcode(String var1, String var2, String var3, String var4, String var5, String var6, String var7, String var8, String var9);

        int printerfont(String var1, String var2, String var3, String var4, String var5, String var6, String var7);

        int clearbuffer();

        int printlabel(String var1, String var2);

        int formfeed();

        int nobackfeed();

        int windowsfont(int var1, int var2, int var3, int var4, int var5, int var6, String var7, String var8);
    }
}
